﻿namespace Microsoft.SharePoint.Client.WebParts
{
    using Microsoft.SharePoint.Client;
    using System;
    using System.ComponentModel;

    [ScriptType("SP.WebParts.WebPartDefinition", ServerTypeId="{44bf1024-6127-432a-8e3d-fb317fb4541e}")]
    public class WebPartDefinition : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public WebPartDefinition(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public void CloseWebPart()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "CloseWebPart", null);
            base.Context.AddQuery(query);
        }

        [Remote]
        public void DeleteWebPart()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "DeleteWebPart", null);
            base.Context.AddQuery(query);
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                string str = peekedName;
                if (str == null)
                {
                    return flag;
                }
                if (!(str == "Id"))
                {
                    if (str == "WebPart")
                    {
                        flag = true;
                        reader.ReadName();
                        this.WebPart.FromJson(reader);
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["Id"] = reader.ReadGuid();
            }
            return flag;
        }

        [Remote]
        public void MoveWebPartTo(string zoneID, int zoneIndex)
        {
            if (base.Context.ValidateOnClient)
            {
                if (zoneID == null)
                {
                    throw ClientUtility.CreateArgumentNullException("zoneID");
                }
                if ((zoneID != null) && (zoneID.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("zoneID");
                }
                if ((zoneID != null) && (zoneID.Length > 0x40))
                {
                    throw ClientUtility.CreateArgumentException("zoneID");
                }
                if (zoneIndex < 0)
                {
                    throw ClientUtility.CreateArgumentException("zoneIndex");
                }
            }
            ClientAction query = new ClientActionInvokeMethod(this, "MoveWebPartTo", new object[] { zoneID, zoneIndex });
            base.Context.AddQuery(query);
        }

        [Remote]
        public void OpenWebPart()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "OpenWebPart", null);
            base.Context.AddQuery(query);
        }

        [Remote]
        public void SaveWebPartChanges()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "SaveWebPartChanges", null);
            base.Context.AddQuery(query);
        }

        [Remote]
        public Guid Id
        {
            get
            {
                base.CheckUninitializedProperty("Id");
                return (Guid) base.ObjectData.Properties["Id"];
            }
        }

        [Remote]
        public Microsoft.SharePoint.Client.WebParts.WebPart WebPart
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("WebPart", out obj2))
                {
                    return (Microsoft.SharePoint.Client.WebParts.WebPart) obj2;
                }
                Microsoft.SharePoint.Client.WebParts.WebPart part = new Microsoft.SharePoint.Client.WebParts.WebPart(base.Context, new ObjectPathProperty(base.Context, base.Path, "WebPart"));
                base.ObjectData.ClientObjectProperties["WebPart"] = part;
                return part;
            }
        }
    }
}

